package com.eco.fanliapp.controller.app;


import com.eco.fanliapp.common.base.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.service.IAppUserService;
import com.eco.fanliapp.service.ISyUserCollectionService;
import lombok.Synchronized;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 收藏表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/app/user")
public class SyUserCollectionController extends BaseController {

    @Autowired
    private ISyUserCollectionService syUserCollectionService;
    @Autowired
    private IAppUserService appUserService;


    /**
     * 用户收藏列表-used
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping(value = "/collection/list")
    @ResponseBody
    public ServerResponse collectionList(@RequestParam(defaultValue = "20") Integer pageSize, @RequestParam(defaultValue = "1") Integer pageNum) {

        AppUser u = getAppUser();
        if (u == null || u.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301, "用户不存在");
        }
        return syUserCollectionService.collectionList(u.getUserId(), pageNum, pageSize);
    }

    /**
     * 是否已经收藏
     * @param itemId
     * @return
     */
    @GetMapping(value = "/collection/goodsCollection")
    @ResponseBody
    public ServerResponse goodsCollection(Long itemId) {
        AppUser u = getAppUser();
        if (u == null || u.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301, "用户不存在");
        }
        return syUserCollectionService.goodsCollection(u.getUserId(), itemId);
    }

    //

    /**
     * 去收藏/取消收藏
     * @param itemId
     * @param type 0 去收藏 1 取消收藏
     * @return
     */
    @Synchronized
    @GetMapping(value = "/collection/toDo")
    @ResponseBody
    public ServerResponse toDo(Long itemId, Integer type) {
        AppUser u = getAppUser();
        if (u == null || u.getUserId() == null) {
            return ServerResponse.createBySuccessCodeMessage(301, "用户不存在");
        }
        if (type == 0) {
            //去收藏
            return syUserCollectionService.saveCollection(u.getUserId(), itemId, type);
        } else if (type == 1) {
            //取消收藏
            return syUserCollectionService.cancelCollection(u.getUserId(), itemId, type);
        } else {
            return null;
        }
    }
}
